package mi.payment.ui.qr.code;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import mi.payment.R;
import mi.payment.bean.db.TradeData;
import mi.payment.settings.TransactionSwitchSetting;
import mi.payment.transaction.sale.code.ScanPaymentController;
import mi.payment.db.dao.PayDetail;
import mi.payment.settings.TransactionSwitchOperation;
import mi.payment.transaction.sale.code.ScanActivity;
import mi.payment.transaction.AutoFinishActivity;
import mi.payment.utils.QRCodeTypeUtils;
import sunmi.sunmiui.title.Title;

import static mi.payment.utils.QRCodeTypeUtils.TYPE_Alipay;
import static mi.payment.utils.QRCodeTypeUtils.TYPE_Mipay;
import static mi.payment.utils.QRCodeTypeUtils.TYPE_No;
import static mi.payment.utils.QRCodeTypeUtils.TYPE_UNION;
import static mi.payment.utils.QRCodeTypeUtils.TYPE_WeChat;

/**
 * @author longx on 2017/3/23.
 */

public class EnterPaymentCodeActivity extends AutoFinishActivity {

    public static final String QR_CODE_PAY_TRADE_DATA = "trade_data";

    private EditText qrCodeEdit;
    private TradeData tradeData;

    private ScanPaymentController scanPaymentHelper;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setAutoFinish(false);
        setContentView(R.layout.activity_enter_payment_code);
        initView();
    }

    private void initView() {
        Title title = (Title) this.findViewById(R.id.head);
        title.setHeadBgColor(Color.parseColor("#fd5a52"));
        title.setTitleText(getString(R.string.enter_payment_code));
        title.setButtonVisibility(false, true, true, true);
        title.setMenuClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }

        }, null, null, null);
        findViewById(R.id.rel_sure_to_consume).setOnClickListener(this);
        qrCodeEdit = (EditText) findViewById(R.id.edit_payment_code);
        tradeData = (TradeData) getIntent().getExtras().getSerializable(EnterPaymentCodeActivity.QR_CODE_PAY_TRADE_DATA);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rel_sure_to_consume:
                if (TextUtils.isEmpty(qrCodeEdit.getText())) {
                    showToast(getString(R.string.empty_qr_code));
                } else {
                    startTransaction(qrCodeEdit.getText().toString());
                }
                break;
        }
    }

    /**
     * 相关说明见@see {@link ScanActivity} 的isValidQrCode(String)方法
     */
    protected void startTransaction(String qrCode) {
        TransactionSwitchOperation operation = TransactionSwitchOperation.getInstance();
        TransactionSwitchSetting setting = operation.getTransactionSwitchSetting();
        PayDetail payDetail = tradeData.getPayDetail();
        payDetail.setPayQRCode(qrCode);
        if (payDetail.getTransPlatform() == ScanConfig.PLATFORM_ALL)//主扫合并
            switch (QRCodeTypeUtils.checkQRCodeType(qrCode)) {
                case TYPE_Alipay:
                    if (setting.isUseAliPayScan()) {
                        payDetail.setTransPlatform(1);
                        break;//true
                    } else {
                        showToast(getString(R.string.nonsupport_scan_code_alipay));//如果支付宝开关是关闭的，则提示用户
                        return;//false
                    }
                case TYPE_WeChat:
                    if (setting.isUseWeChatScan()) {
                        payDetail.setTransPlatform(2);
                        break;//true
                    } else {
                        showToast(getString(R.string.nonsupport_scan_code_wechat));
                        return;//false
                    }
                case TYPE_UNION:
                    if (setting.isUseUnionPayScan()) {
                        payDetail.setTransPlatform(4);
                        break;
                    } else {
                        showToast(getString(R.string.nonsupport_scan_code_union));
                        break;
                    }
                case TYPE_Mipay:
                    if (setting.isUseMiPay()) {
                        payDetail.setTransPlatform(5);
                        break;
                    } else {
                        showToast(getString(R.string.nonsupport_scan_code_mipay));
                        break;
                    }
                case TYPE_No:
                default:
                    //其他二维码
                    showToast(getString(R.string.invalid_qr_code));
                    return;//false
            }
        scanPaymentHelper = new ScanPaymentController(this, tradeData);
        scanPaymentHelper.scanSuccess();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (scanPaymentHelper != null) {
            scanPaymentHelper.onActivityDestroy();
        }
    }
}
